//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "OrthogonalizedBumpFinder.h"
using namespace Cephei::QL::Models::Marketmodels::Pathwisegreeks;
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Models/Marketmodels/Pathwisegreeks/VegaBumpCollection.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Pathwisegreeks::COrthogonalizedBumpFinder::COrthogonalizedBumpFinder (boost::shared_ptr<QuantLib::OrthogonalizedBumpFinder>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOrthogonalizedBumpFinder = NULL;
#endif
	_ppOrthogonalizedBumpFinder = &childNative;
    
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::COrthogonalizedBumpFinder::COrthogonalizedBumpFinder (QuantLib::OrthogonalizedBumpFinder& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phOrthogonalizedBumpFinder = NULL;
#endif
	_ppOrthogonalizedBumpFinder = new boost::shared_ptr<QuantLib::OrthogonalizedBumpFinder> (&childNative);
    
    _OrthogonalizedBumpFinderOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::Pathwisegreeks::COrthogonalizedBumpFinder::COrthogonalizedBumpFinder (COrthogonalizedBumpFinder^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOrthogonalizedBumpFinder = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppOrthogonalizedBumpFinder = new boost::shared_ptr<QuantLib::OrthogonalizedBumpFinder> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::COrthogonalizedBumpFinder::COrthogonalizedBumpFinder (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOrthogonalizedBumpFinder = NULL;
#endif
	if (!t->IsSubclassOf(COrthogonalizedBumpFinder::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Pathwisegreeks::COrthogonalizedBumpFinder::COrthogonalizedBumpFinder (QuantLib::Handle<QuantLib::OrthogonalizedBumpFinder>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phOrthogonalizedBumpFinder = &childNative;
	_ppOrthogonalizedBumpFinder = &static_cast<boost::shared_ptr<QuantLib::OrthogonalizedBumpFinder>>(childNative.currentLink());
    
    _OrthogonalizedBumpFinderOwner = owner;
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::COrthogonalizedBumpFinder::COrthogonalizedBumpFinder (QuantLib::Handle<QuantLib::OrthogonalizedBumpFinder> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phOrthogonalizedBumpFinder = &childNative;
	_ppOrthogonalizedBumpFinder = &static_cast<boost::shared_ptr<QuantLib::OrthogonalizedBumpFinder>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Pathwisegreeks::COrthogonalizedBumpFinder::COrthogonalizedBumpFinder (QuantLib::OrthogonalizedBumpFinder childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOrthogonalizedBumpFinder = NULL;
#endif
	_ppOrthogonalizedBumpFinder = new boost::shared_ptr<QuantLib::OrthogonalizedBumpFinder> (new QuantLib::OrthogonalizedBumpFinder (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::Pathwisegreeks::COrthogonalizedBumpFinder::~COrthogonalizedBumpFinder ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppOrthogonalizedBumpFinder != NULL)
    {
	    delete _ppOrthogonalizedBumpFinder;
        _ppOrthogonalizedBumpFinder = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::COrthogonalizedBumpFinder::!COrthogonalizedBumpFinder ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppOrthogonalizedBumpFinder != NULL)
    {
	    delete _ppOrthogonalizedBumpFinder;
    }
}
QuantLib::OrthogonalizedBumpFinder& Cephei::QL::Models::Marketmodels::Pathwisegreeks::COrthogonalizedBumpFinder::GetReference ()
{
    if (_ppOrthogonalizedBumpFinder == NULL) throw REFNEW NativeNullException ();
	return **_ppOrthogonalizedBumpFinder;
}
boost::shared_ptr<QuantLib::OrthogonalizedBumpFinder>& Cephei::QL::Models::Marketmodels::Pathwisegreeks::COrthogonalizedBumpFinder::GetShared ()
{
    if (_ppOrthogonalizedBumpFinder == NULL) throw REFNEW NativeNullException ();
	return *_ppOrthogonalizedBumpFinder;
}
QuantLib::OrthogonalizedBumpFinder* Cephei::QL::Models::Marketmodels::Pathwisegreeks::COrthogonalizedBumpFinder::GetPointer ()
{
    if (_ppOrthogonalizedBumpFinder == NULL) throw REFNEW NativeNullException ();
	return &**_ppOrthogonalizedBumpFinder;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::OrthogonalizedBumpFinder>& Cephei::QL::Models::Marketmodels::Pathwisegreeks::COrthogonalizedBumpFinder::GetHandle ()
{
	if (_phOrthogonalizedBumpFinder == NULL)
	{
		_phOrthogonalizedBumpFinder = new Handle<QuantLib::OrthogonalizedBumpFinder> (*_ppOrthogonalizedBumpFinder);
	}
	return *_phOrthogonalizedBumpFinder;
}
#endif
bool Cephei::QL::Models::Marketmodels::Pathwisegreeks::COrthogonalizedBumpFinder::HasNative () 
{
	return (_ppOrthogonalizedBumpFinder != NULL);
}

Cephei::QL::Models::Marketmodels::Pathwisegreeks::IOrthogonalizedBumpFinder^ Cephei::QL::Models::Marketmodels::Pathwisegreeks::COrthogonalizedBumpFinder::GetVegaBumps (Cephei::Core::IMatrix<Cephei::QL::Math::IMatrix^>^ theBumps)
{
    try
    {
        theBumps->Lock();
        INativeMatrix<Cephei::QL::Math::IMatrix^>^ _NCItheBumps = theBumps->getFeature (NativeFeature::Value);
        CMatrixMatrix^ _NCtheBumps = safe_cast<CMatrixMatrix^>(_NCItheBumps);
        std::vector<std::vector<QuantLib::Matrix> >& _theBumps = static_cast<std::vector<std::vector<QuantLib::Matrix> >&> (_NCtheBumps->GetReference ());
    	(*_ppOrthogonalizedBumpFinder)->GetVegaBumps ( _theBumps );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (theBumps != nullptr) theBumps->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

